package com.huxiu.pagers.qunzu;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.huxiu.R;
import com.huxiu.base.ZiXunDetailPager;
import com.huxiu.listener.ListenerConstans;
import com.huxiu.listener.ViewPagerListener;
import com.huxiu.utils.Settings;
import com.huxiu.utils.UmenEvent;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;


public class FaXian extends ZiXunDetailPager implements ViewPagerListener {


    //    @ViewInject(R.id.lv_tabdetail_list)
    //    private RefreshListView lv_tabdetail_list;// 新闻列表
    @ViewInject(R.id.vp_content_pager)
    private ViewPager vp_content_pager;

    @ViewInject(R.id.rg_content_bottom)
    private RadioGroup rg_content_bottom;

    private List<ZiXunDetailPager> pagers;

    public FaXian(Context context) {
        super(context);
    }
    //监听器，用于回调
    @Override
    public void setCurrentItem(int page, ViewPagerListener mViewPagerListener) {
        vp_content_pager.setCurrentItem(page);
    }

    @Override
    public void setCurrentItem(int page) {
        vp_content_pager.setCurrentItem(page);
        rg_content_bottom.check(R.id.rb_qunzu);
    }
    @Override
    protected View initView() {
        View viewAll = View.inflate(mContext, R.layout.qunzu_faxian, null);
        ViewUtils.inject(this, viewAll);
        ListenerConstans.mFaXian = this;
        // 给下拉刷新Listview设置监听器
        //        lv_tabdetail_list.setOnRefreshingListener(new MyOnRefreshingListener());
        // 监听新闻条目点击事件
        //        lv_tabdetail_list.setOnItemClickListener(new MyOnItemClickListener());
        return viewAll;
    }


    @Override
    public void initData() {
        pagers = new ArrayList<ZiXunDetailPager>();
        pagers.add(new HuaTiDetailFaXian(mContext));
        pagers.add(new QunZuDetailFaXian(mContext));
        // 更新ViewPager
        vp_content_pager.setAdapter(new MyAdapter());
        // 监听ViewPager
        vp_content_pager.setOnPageChangeListener(new MyOnPageChangeListener());
        // 初始化首页，加载数据
        pagers.get(0).initData();
        // 监听底部单选按钮
        rg_content_bottom.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        // 设置默认选中首页
        rg_content_bottom.check(R.id.rb_huati);
    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_huati:
                    vp_content_pager.setCurrentItem(0, false);// 参数2，是否带动画
                    break;
                case R.id.rb_qunzu:
                    vp_content_pager.setCurrentItem(1, false);
                    break;
            }
        }

    }


    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // 当用户切换到某一页时，再加载数据
            pagers.get(position).initData();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (!Settings.isLoggedIn()) {
                switch (position){
                    case 0:
                        MobclickAgent.onEvent(mContext, UmenEvent.GROUP_UNLOGIN_TOPIC);
                        break;
                    case 1:
                        MobclickAgent.onEvent(mContext, UmenEvent.GROUP_UNLOGIN_GROUP);
                        break;
                }

            }else{
                switch (position){
                    case 0:
                        MobclickAgent.onEvent(mContext, UmenEvent.GROUP_LOGIN_TOPIC);
                        break;
                    case 1:
                        MobclickAgent.onEvent(mContext, UmenEvent.GROUP_LOGIN_GROUP);
                        break;
                }
                MobclickAgent.onEvent(mContext, UmenEvent.GROUP_PAGE_LOGIN_MY);
            }
            ZiXunDetailPager basePager = pagers.get(position);
            // 返回控件
            container.addView(basePager.rootView);

            // 更新界面的数据，预加载功能，防止加载数据浪费流量
            //			basePager.initData();
            return basePager.rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
 